package com.gr.staffpm;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.subject.Subject;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.protocol.http.WebSession;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.gr.menuw.common.StringModel;
import com.gr.menuw.horizontal.dropdown.custom.CustomMenu;
import com.gr.menuw.horizontal.dropdown.custom.CustomSubMenu;
import com.gr.menuw.menu.item.LinkMenuItem;
import com.gr.staffpm.pages.admin.AdminSystemPage;
import com.gr.staffpm.pages.admin.role.AdminRolesPage;
import com.gr.staffpm.pages.admin.user.AdminUsersPage;
import com.gr.staffpm.pages.behaviour.AppLoadingIndicatorBehaviour;
import com.gr.staffpm.pages.dashboard.Dashboard;
import com.gr.staffpm.pages.management.feedback.TeamFeedbackPage;
import com.gr.staffpm.pages.management.resources.team.TeamResourcesPage;
import com.gr.staffpm.pages.management.review.TaskReviewPage;
import com.gr.staffpm.pages.projects.Projects;
import com.gr.staffpm.pages.schedule.Schedule;
import com.gr.staffpm.properties.PropertiesController;
import com.gr.staffpm.security.constants.StaffPMRoles;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.utils.DateTimeUtils;

@RequiresAuthentication
public abstract class StaffPMWebPage extends WebPage {

    @SpringBean(name = "userService")
    private UserService userService;

    public StaffPMWebPage() {
        add(new AppLoadingIndicatorBehaviour());
        add(new HeaderPanel(userService, "header"));
        add(buildMenu());

        Label lastLogin;
        String lLoginDate = DateTimeUtils.toHumanFormat(((StaffPMSession) WebSession.get()).getLastLogin());
        if (lLoginDate != null && !lLoginDate.equals(" ago"))
            lastLogin = new Label("lastLogin", new StringModel("Last login: " + lLoginDate));
        else
            lastLogin = new Label("lastLogin", new StringModel("This is your first time logging into StaffPM!"));
        add(lastLogin);
    }

    @Override
    protected void onBeforeRender() {
        if (get("title") == null)
            add(new Label("title", getTitle()));
        super.onBeforeRender();
    }

    public abstract String getTitle();

    private CustomMenu buildMenu() {

        boolean feedbackOnly = Boolean.parseBoolean(PropertiesController.getInstance().getProperty("feedback.module.only", "false"));
        CustomMenu menu = new CustomMenu("menu");

        menu.addItem(new LinkMenuItem("1", "Dashboard", Dashboard.class));
        if (!feedbackOnly) {
            menu.addItem(new LinkMenuItem("2", "Projects", Projects.class));
            menu.addItem(new LinkMenuItem("3", "My Schedule", Schedule.class));
        }
        menu.addItem(new LinkMenuItem("4", "Feedback", TeamFeedbackPage.class));

        Subject currentUserSubject = SecurityUtils.getSubject();
        if ((currentUserSubject.hasRole(StaffPMRoles.MANAGER) || currentUserSubject.hasRole(StaffPMRoles.ADMIN)) && !feedbackOnly) {
            CustomSubMenu subMenu = new CustomSubMenu("5", "Management", menu);

            subMenu.addItem(new LinkMenuItem("6", "Task Review Queue", TaskReviewPage.class));
            subMenu.addItem(new LinkMenuItem("7", "Team Resources", TeamResourcesPage.class));
            menu.addItem(subMenu);
        }
        if (currentUserSubject.hasRole(StaffPMRoles.ADMIN)) {
            CustomSubMenu subMenu = new CustomSubMenu("8", "Administration", menu);

            subMenu.addItem(new LinkMenuItem("9", "User Management", AdminUsersPage.class));
            subMenu.addItem(new LinkMenuItem("10", "Role Management", AdminRolesPage.class));
            subMenu.addItem(new LinkMenuItem("11", "System Management", AdminSystemPage.class));
            menu.addItem(subMenu);
        }

        return menu;
    }
}
